/**
 * Project Name:HomeActivity
 * Date:2016-3-3上午10:22:34
 * Copyright (c) 2016, askzhao All Rights Reserved.
 *
 */
package com.kkd.kkdapp.adapter;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.KKDWebActivity;
import com.kkd.kkdapp.http.entity.HomeRecommend;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.utils.SystemUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * ClassName: BannerAdapter <br/>
 * date: 2016-3-3 上午10:22:34 <br/>
 * 
 * @author askzhao
 * @version
 */
public class BannerAdapter extends PagerAdapter {
	private ArrayList<HomeRecommend> mImageUrlList;
	private Context context;

	public BannerAdapter(Context context, ArrayList<HomeRecommend> data) {
		this.context = context;
		this.mImageUrlList = data;
	}

	@Override
	public int getCount() {
		return mImageUrlList.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	public void destroyItem(android.view.ViewGroup container, int position,
			Object object) {
		container.removeView((View) object);
	}

	@Override
	public Object instantiateItem(ViewGroup container, final int position) {
		RelativeLayout layout = new RelativeLayout(context);
		layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));

		ImageView imageView = new ImageView(context);
		imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		imageView.setScaleType(ImageView.ScaleType.FIT_XY);
		try {
			ImageLoader.getInstance().displayImage(
					mImageUrlList.get(position).getImage(), imageView,
					R.drawable.home_ad);
		} catch (Exception e) {
			e.printStackTrace();
		}
		imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					if (!TextUtils
							.isEmpty(mImageUrlList.get(position).getUrl())) {
						SystemUtils.onEvent("Regular_Banner_" + (position + 1),
								1);
						Intent intent = new Intent(context,
								KKDWebActivity.class);
						intent.putExtra("useTitleName", true);
						intent.putExtra("titleName", mImageUrlList
								.get(position).getTitle());
						intent.putExtra("desc", mImageUrlList.get(position)
								.getDesc());
						String url = mImageUrlList.get(position).getUrl();
						intent.putExtra("url", mImageUrlList.get(position)
								.getUrl());
						intent.putExtra("shareUrl", mImageUrlList.get(position)
								.getShareUrl());
						context.startActivity(intent);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});
		layout.addView(imageView);

		container.addView(layout);
		return layout;
	}
}